package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.FinalAccountFeeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@AttrHandler(FinalAccountFeeAttrHandler.class)
public class FinalAccountFeeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    OperateInstAttrService operateInstAttrService;
    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if ("sceneTempl".equals(data.getExtAttributes().get("entry")) || (KiteMapUtils.isNotEmpty(extParams) && extParams.containsKey("nodeCode"))) {
            return;
        }
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        //变更业务的场景初始化过程中不进行计算
        if (ServiceOfferConsts.SERVICE_OFFER_MODIFY.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), "initSource"))) {
            return;
        }
        String fieldName = attr.getFieldName();
        if(KiteStringUtils.equals("200004184",fieldName)){
            String acctFee ="";
            KiteProdInst prodInst = querySceneInstService.getProdInstByFuncProdInstId(data.getCustId(),inst.getInstId());
            if(KiteObjectUtils.isEmpty(prodInst)){
                return;
            }
            List<KiteProdInstFunc> funcs = querySceneInstService.listProdInstFuncsBySceneInstId(prodInst.getProdInstId(),data.getSceneInstId());
            if(KiteListUtils.isNotEmpty(funcs)){
                for(KiteProdInstFunc func : funcs){
                    //通信套包类型
                    if(!KiteStringUtils.equals(inst.getInstId(),func.getInstId())){
                        KiteAttr acctFeeAttr = func.getAttr("200004183");
                        if(!KiteObjectUtils.isEmpty(acctFeeAttr)){
                            if(!KiteStringUtils.equals(LocalKeyConsts.KITE_ATTR_CONFIG_SHOW_TYPE_D,acctFeeAttr.getShowType())){
                                acctFee = "0";
                            }else{
                                if(KiteStringUtils.isEmpty(acctFee)){
                                    acctFee = acctFeeAttr.getValue();
                                }else{
                                    acctFee = StrTools.numAdd(acctFee,acctFeeAttr.getValue());
                                }
                            }
                        }
                    }
                }
            }
            if(KiteStringUtils.equals("0",acctFee)){
                operateInstAttrService.setAttrValue(inst, "200001694", "0", data.getSceneInstId(), null);
            }else{
                String discountRate = StrTools.numDivide(attr.getValue(), acctFee,4);
                discountRate = StrTools.numMultiply(discountRate,"100");
                discountRate = StrTools.numIntRoundUp(discountRate,2);
                operateInstAttrService.setAttrValue(inst, "200001694", discountRate, data.getSceneInstId(), null);
            }
        }
    }


    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
